package com.ruoyi.web.controller.net;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.net.domain.NetElementPort;
import com.ruoyi.net.service.INetElementPortService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.net.domain.NetElement;
import com.ruoyi.net.service.INetElementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 网元设备Controller
 * 
 * @author ruoyi
 * @date 2024-10-27
 */
@Api(tags = "网元设备")
@RestController
@RequestMapping("/net/element")
public class NetElementController extends BaseController
{
    @Autowired
    private INetElementService netElementService;

    @Autowired
    private INetElementPortService netElementPortService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询所有网元设备状态
     */
    @ApiOperation(value = "查询所有网元设备状态", httpMethod = "GET")
    @GetMapping("/status")
    public AjaxResult status(String id) {
        List<String> statusList = new ArrayList<>();
        Collection<String> keys = redisCache.keys(CacheConstants.NET_ELEMENT_STATUS + "*");
        for (String key : keys) {
            String netStatus = redisCache.getCacheObject("key");
            statusList.add(key.replaceAll(CacheConstants.NET_ELEMENT_STATUS, "") + "," + netStatus.split(",")[0]);
        }
        return success(statusList);
    }

    /**
     * 查询网元设备列表
     */
    @ApiOperation(value = "查询网元设备列表", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('net:element:list')")
    @GetMapping("/list")
    public TableDataInfo list(NetElement netElement)
    {
        startPage();
        List<NetElement> list = netElementService.selectNetElementList(netElement);
        return getDataTable(list);
    }

    /**
     * 导出网元设备列表
     */
    @ApiOperation(value = "导出网元设备列表", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('net:element:export')")
    @Log(title = "网元设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NetElement netElement)
    {
        List<NetElement> list = netElementService.selectNetElementList(netElement);
        ExcelUtil<NetElement> util = new ExcelUtil<NetElement>(NetElement.class);
        util.exportExcel(response, list, "网元设备数据");
    }

    /**
     * 获取网元设备详细信息
     */
    @ApiOperation(value = "获取网元设备详细信息", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('net:element:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(netElementService.selectNetElementById(id));
    }

    /**
     * 新增网元设备
     */
    @ApiOperation(value = "新增网元设备", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('net:element:add')")
    @Log(title = "网元设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NetElement netElement)
    {
        return toAjax(netElementService.insertNetElement(netElement));
    }

    /**
     * 修改网元设备
     */
    @ApiOperation(value = "修改网元设备", httpMethod = "PUT")
    @PreAuthorize("@ss.hasPermi('net:element:edit')")
    @Log(title = "网元设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NetElement netElement)
    {
        return toAjax(netElementService.updateNetElement(netElement));
    }

    /**
     * 删除网元设备
     */
    @ApiOperation(value = "删除网元设备", httpMethod = "DELETE")
    @PreAuthorize("@ss.hasPermi('net:element:remove')")
    @Log(title = "网元设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        // todo 判断网元是否存在已绑定端口
        for (String id : ids) {
            NetElement netElement = netElementService.selectNetElementById(id);
            if (netElement == null) {
                return AjaxResult.error("网元设备不存在");
            }
            /*NetElementPort netElementPort = new NetElementPort();
            netElementPort.setElementId(id);
            netElementPort.setBindStatus("1");
            if (!netElementPortService.selectNetElementPortList(netElementPort).isEmpty()){
                return AjaxResult.error("网元设备存在已绑定端口，无法删除");
            }*/
        }
        // TODO: 2024/11/2 删除网元设备时，需要判断网元设备端口是否有效
        if(netElementPortService.checkPortBindStatusByElementId(ids)) {
            return AjaxResult.error("网元设备存在已绑定端口，无法删除");
        }
        return toAjax(netElementService.deleteNetElementByIds(ids));
    }

}
